load_kernel3:
	call os_clear_screen; Ready to read first block of data
	mov ax, 19			; Root dir starts at logical sector 19
	call l2hts

	mov si, buffer3			; Set ES:BX to point to our buffer (see end of code)
	mov bx, ds
	mov es, bx
	mov bx, si

	mov ah, 2			; Params for int 13h: read floppy sectors
	mov al, 14			; And read 14 of them

	pusha				; Prepare to enter loop

read_root_dir3:
	popa				; In case registers are altered by int 13h
	pusha

	stc				; A few BIOSes do not set properly on error
	int 13h				; Read sectors using BIOS

	jnc search_dir3			; If read went OK, skip ahead
	call reset_floppy		; Otherwise, reset floppy controller and try again
	jnc read_root_dir3		; Floppy reset OK?

	mov ax, 0
	int 19h				; Reboot the system

search_dir3:
	popa

	mov ax, ds			; Root dir is now in [buffer]
	mov es, ax			; Set DI to this info
	mov di, buffer3

	mov cx, word [RootDirEntries]	; Search all (224) entries
	mov ax, 0			; Searching at offset 0

next_root_entry3:
	xchg cx, dx			; We use CX in the inner loop...

	mov si,mikeos2		; Start searching for kernel filename
	mov cx, 11
	rep cmpsb
	je found_file_to_load3		; HiddenSectors DI will be at offset 11

	add ax, 32			; Bump searched entries by 1 (32 bytes per entry)

	mov di, buffer3			; Point to next entry
	add di, ax

	xchg dx, cx			; Get the original CX back
	loop next_root_entry3
	
	mov si, file_not_found                ; If kernel is not found, bail out
    call os_print_string
	call os_wait_for_key
	jmp stage_2_boot_menu.stuff_done
found_file_to_load3:			; FetchDriveNo and load FAT into RAM
	mov ax, word [es:di+0Fh]	; Offset 11 + 15 = 26, contains 1stDriveNo
	mov word [DriveNo], ax

	mov ax, 1			; Sector 1 = first sector of first FAT
	call l2hts

	mov di, buffer3			; ES:BX points to our buffer
	mov bx, di

	mov ah, 2			; int 13h params: read (FAT) sectors
	mov al, 9			; All 9 sectors of 1st FAT

	pusha				; Prepare to enter loop

read_fat3:
	popa				; In case registers are altered by int 13h
	pusha

	stc
	int 13h				; Read sectors using the BIOS

	jnc read_fat_ok3		; If read went OK, skip ahead
	call reset_floppy		; Otherwise, reset floppy controller and try again
	jnc read_fat3			; Floppy reset OK?

	mov ax, 0
	int 19h				; Reboot the system

read_fat_ok3:
	popa

	mov ax, 2000h			; Segment where we'll load the kernel
	mov es, ax
	mov bx, 0

	mov ah, 2			; int 13h floppy read params
	mov al, 1

	push ax				; Save in case we (or int calls) lose it

load_file_sector3:
	mov ax, word [DriveNo]		; Convert sector to logical
	add ax, 31

	call l2hts			; Make appropriate params for int 13h

	mov ax, 2000h			; Set buffer past what we've already read
	mov es, ax
	mov bx, word [HiddenSectors]

	pop ax				; Save in case we (or int calls) lose it
	push ax

	stc
	int 13h

	jnc calculate_next_cluster3	; If there's no error...

	call reset_floppy		; Otherwise, reset floppy and retry
	jmp load_file_sector3

calculate_next_cluster3:
	mov ax, [DriveNo]
	mov dx, 0
	mov bx, 3
	mul bx
	mov bx, 2
	div bx				; DX = [cluster] mod 2
	mov si, buffer3
	add si, ax			; AX = word in FAT for the 12 bit entry
	mov ax, word [ds:si]

	or dx, dx			; If DX = 0 [cluster] is even; if DX = 1 then it's odd

	jz even3				; If [cluster] is even, drop last 4 bits of word
					; with nextDriveNo; if odd, drop first 4 bits

odd3:
	shr ax, 4			; Shift out first 4 bits (they belong to another entry)
	jmp short next_cluster_cont3


even3:
	and ax, 0FFFh			; Mask out final 4 bits

next_cluster_cont3:
	mov word [DriveNo], ax		; StoreDriveNo

	cmp ax, 0FF8h			; FF8h = end of file marker in FAT12
	jae end

	add word [HiddenSectors], 512	; Increase buffer HiddenSectors 1 sector length
	jmp load_file_sector3

end3:	
	mov si, mikeos2
	mov bl, 0
	mov byte [si], bl
					; We've got the file to load!
	pop ax				; Clean up the stack (AX was pushed earlier)
	mov dl, byte [VolumeID]		; Provide kernel with boot device info

	cli				; Clear interrupts
	mov ax, 0
	mov ss, ax			; Set stack segment and HiddenSectors
	mov sp, 0F000h
	sti				; Restore interrupts

	cld				; The default direction for string operations
					; will be 'up' - incrementing address in RAM

	mov ax, 2000h			; Set all segments to match where kernel is loaded
	mov ds, ax			; After this, we don't need to bother with
	mov es, ax			; segments ever again!
	mov fs, ax
	mov gs, ax

	jmp 2000h:0000h			; Jump to entry point of loaded kernel!
	
	mikeos2 db "USERKRNLEXE"
	file_not_found db 'USERKRNL.EXE Not found!.',13,10,'Please install dosmode or MiniDOS',0
buffer3: